package com.yonyou.ucf.mdf.app.isvdxq.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ClientAddressUtil {
    private static final Logger log = LoggerFactory.getLogger(ClientAddressUtil.class);
    private static final String UNKNOWN = "unknown";
    private static final Pattern DOMAIN_PATTERN = Pattern.compile("[\\w-]+\\.(com.cn|net.cn|gov.cn|org\\.nz|org.cn|com|net|org|gov|cc|biz|info|cn|co)\\b()*");

    public ClientAddressUtil() {
    }

    public static String getRootDomain(HttpServletRequest request) {
        String domain = request.getServerName();
        Matcher matcher = DOMAIN_PATTERN.matcher(domain);
        if (matcher.find()) {
            domain = matcher.group();
        }

        return domain;
    }

    public static String getLatestProxyIpAddress(HttpServletRequest request) {
        String xRealIP = request.getHeader("X-Real-IP");
        String xForwardIp = request.getHeader("X-Forwarded-For");
        log.debug("X-Real-IP: {}, X-Forwarded-For: {}", xRealIP, xForwardIp);
        if (StringUtils.isNotEmpty(xForwardIp) && !"unknown".equalsIgnoreCase(xForwardIp)) {
            String[] ips = xForwardIp.split(",");
            int needIpIndex = ips.length - 2;
            if (needIpIndex < 0) {
                needIpIndex = ips.length - 1;
            }

            return ips[needIpIndex];
        } else {
            return getClientIpAddress(request);
        }
    }

    public static String getClientIpAddress(HttpServletRequest request) {
        String xRealIP = request.getHeader("X-Real-IP");
        String xForwardIp = request.getHeader("X-Forwarded-For");
        log.debug("X-Real-IP: {}, X-Forwarded-For: {}", xRealIP, xForwardIp);
        if (StringUtils.isNotEmpty(xForwardIp) && !"unknown".equalsIgnoreCase(xForwardIp)) {
            int index = xForwardIp.indexOf(",");
            return index != -1 ? xForwardIp.substring(0, index) : xForwardIp;
        } else {
            xForwardIp = xRealIP;
            if (StringUtils.isNotEmpty(xRealIP) && !"unknown".equalsIgnoreCase(xRealIP)) {
                return xRealIP;
            } else {
                if (StringUtils.isBlank(xRealIP) || "unknown".equalsIgnoreCase(xRealIP)) {
                    xForwardIp = request.getHeader("Proxy-Client-IP");
                }

                if (StringUtils.isBlank(xForwardIp) || "unknown".equalsIgnoreCase(xForwardIp)) {
                    xForwardIp = request.getHeader("WL-Proxy-Client-IP");
                }

                if (StringUtils.isBlank(xForwardIp) || "unknown".equalsIgnoreCase(xForwardIp)) {
                    xForwardIp = request.getHeader("HTTP_CLIENT_IP");
                }

                if (StringUtils.isBlank(xForwardIp) || "unknown".equalsIgnoreCase(xForwardIp)) {
                    xForwardIp = request.getHeader("HTTP_X_FORWARDED_FOR");
                }

                if (StringUtils.isBlank(xForwardIp) || "unknown".equalsIgnoreCase(xForwardIp)) {
                    xForwardIp = request.getRemoteAddr();
                }

                return xForwardIp;
            }
        }
    }
}

